


#include "M120L.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h" 
#include <string.h> 
#include "main.h"
#include "BC26.h"

extern unsigned char UART1_Rx_Buf[MAX_REC_LENGTH];
extern unsigned int  UART1_Rx_cnt ;
extern unsigned char UART1_temp[REC_LENGTH];

extern uint8_t Rece_len;
extern uint8_t Recebuffer[ReceiveUdpDataMaxLen];

uint8_t Valid_Data_Len_Max=50;

extern uint8_t LoRaWAN_Detect;

uint8_t M120_sleep01_cmd[13]={'A','T','+','S','L','E','E','P','=','0','1','\r','\n'};
uint8_t M120_sleep00_cmd[13]={'A','T','+','S','L','E','E','P','=','0','0','\r','\n'};
uint8_t M120_deveui_cmd[12]={'A','T','+','D','E','V','E','U','I','?','\r','\n'};
uint8_t M120_appeui_cmd[12]={'A','T','+','A','P','P','E','U','I','?','\r','\n'};
uint8_t M120_appkey_cmd[12]={'A','T','+','A','P','P','K','E','Y','?','\r','\n'};
uint8_t M120_channel_cmd[13]={'A','T','+','C','H','A','N','N','E','L','?','\r','\n'};
uint8_t M120_join_cmd[10]={'A','T','+','J','O','I','N','?','\r','\n'};
uint8_t M120_enjoin_cmd[18]={'A','T','+','J','O','I','N','=','0','1',',','0','2',',','0','A','\r','\n'}; 
uint8_t M120_reset_cmd[10]={'A','T','+','R','E','S','E','T','\r','\n'};
uint8_t M120_version_cmd[10]={'A','T','+','V','E','R','S','?','\r','\n'};
uint8_t M120_closeADR_cmd[10]={'A','T','+','A','D','R','=','0','\r','\n'};
uint8_t M120_enjoin_cmd_dma[18]={'A','T','+','J','O','I','N','=','0','1',',','0','2',',','0','A','\r','\n'}; 

/*
********************************huang ming 2021.10.19 ************************************************/


/*
*********************************************************************************************************
*	������:   asciitohex
*	����˵��: �����ֽڵ�ASCII��ϳ�1��16��������
*	����ֵ:   16��������
*   ���ߣ�    ����
*   ���ڣ�    2019-06-04
*********************************************************************************************************/
unsigned char asciitohex(unsigned char high_ascii,unsigned char low_ascii)
{
	unsigned char temp_high;
	unsigned char temp_low;
	if(high_ascii<=0x39)
		temp_high = high_ascii - 0x30;
		else
			temp_high = high_ascii - 0x57;
		
	if(low_ascii<=0x39)
		temp_low = low_ascii - 0x30;
		else
			temp_low = low_ascii - 0x57;
	
	
	temp_high = temp_high<<4 | 	temp_low;	
	
	return temp_high;
}

/*
*********************************************************************************************************
*	������:   SendData_To_M120L
*	����˵��: �������ݵ�M120L
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2021-10-19
*********************************************************************************************************/

void SendData_To_M120L(UART_HandleTypeDef *huart,uint8_t *buf,uint8_t len)
{
	HAL_UART_Transmit(huart,buf,len,200);
}



/*
*********************************************************************************************************
*	������:   Get_M120L_Info
*	����˵��: ��ȡM120L�������Ϣ
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2021-10-19
*********************************************************************************************************/

void Get_M120L_Info(void)
{
	SysPrintf("get M120L deveui>>>\r\n");
	UART1_Rx_cnt = 0;
	SendData_To_M120L(&huart1,M120_deveui_cmd,12);
	HAL_Delay(200);
	SysPrintf("len=%d\r\n",UART1_Rx_cnt);
	UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
	SysPrintf("Recv:%s\n",UART1_Rx_Buf);
	
	SysPrintf("get M120L appeui>>>\r\n");
	UART1_Rx_cnt = 0;
	SendData_To_M120L(&huart1,M120_appeui_cmd,12);
	HAL_Delay(200);
	SysPrintf("len=%d\r\n",UART1_Rx_cnt);
	UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
	SysPrintf("Recv:%s\n",UART1_Rx_Buf);
	
	SysPrintf("get M120L appkey>>>\r\n");
	UART1_Rx_cnt = 0;
	SendData_To_M120L(&huart1,M120_appkey_cmd,12);
	HAL_Delay(200);
	SysPrintf("len=%d\r\n",UART1_Rx_cnt);
	UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
	SysPrintf("Recv:%s\n",UART1_Rx_Buf);
	
	SysPrintf("get M120L channel>>>\r\n");
	UART1_Rx_cnt = 0;
	SendData_To_M120L(&huart1,M120_channel_cmd,13);
	HAL_Delay(200);
	SysPrintf("len=%d\r\n",UART1_Rx_cnt);
	UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
	SysPrintf("Recv:%s\n",UART1_Rx_Buf);
}


/*
*********************************************************************************************************
*	������:   M120_PWRON
*	����˵��: ����M120L��Դ
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2021-10-19
*********************************************************************************************************/
void M120_PWRON(void)
{
	
	SysPrintf("M120L power off>>>\r\n");
	NB_PW_OFF;    
	HAL_Delay(5000); 
	SysPrintf("M120L power on>>>\r\n");
	NB_PW_ON;    
	SysPrintf("M120L Initial MCU-IO>>>\r\n");
	SETA_HIGH;
	SysPrintf("M120L internal initial>>>\r\n");
	HAL_Delay(2500);
	SysPrintf("M120L internal initial>>>\r\n");
	HAL_Delay(2500);
	SysPrintf("len=%d\r\n",UART1_Rx_cnt);
}

/*
*********************************************************************************************************
*	������:   M120_PWRON
*	����˵��: ��ʼ��M120L
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2021-10-19
*********************************************************************************************************/
uint8_t M120_Initial(void)
{
	M120_PWRON();
	if(M120_sleep00_INT()==1)
	{
		LoRaWAN_Detect = 1;
		M120_Join_Net_INT();
		return 1;
	}
	else
	{
		LoRaWAN_Detect = 0;
		NB_PW_OFF;
		return 0;
	}
}

/*
*********************************************************************************************************
*	������:   M120_sleep00_INT
*	����˵��: ��M120������ָ��ȴ�M120����
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2021-10-19
*********************************************************************************************************/
int8_t M120_sleep00_INT(void)
{
	SysPrintf("M120L not sleep>>>\r\n");
	UART1_Rx_cnt = 0;
	SendData_To_M120L(&huart1,M120_sleep00_cmd,13);
	HAL_Delay(200);
	SysPrintf("len=%d\r\n",UART1_Rx_cnt);
	UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
	SysPrintf("Recv:%s\n",UART1_Rx_Buf);
	if((strstr(UART1_Rx_Buf,"OK+SLEEP:00")!=NULL))
	{
		SysPrintf("M120L not sleep sucess\r\n"); 
		return 1;
	}
	else
	{
		SysPrintf("M120L not sleep fail\r\n"); 
		return -1;
	}
}

/*
*********************************************************************************************************
*	������:   Get_M120_Status_INT
*	����˵��: M120ģ�������״̬����Ҫ�������ݻ�������֮ǰ�������ѯ������״̬
*	����ֵ:   ��������1������������-1��������������2
*   ���ߣ�    ����
*   ���ڣ�   2021-10-19
*********************************************************************************************************/
int8_t Get_M120_Status_INT(void)
{
	int i;
	SysPrintf("Get M120L join net status>>>\r\n");
	UART1_Rx_cnt=0 ;
	SendData_To_M120L(&huart1,M120_join_cmd,10); 
	HAL_Delay(100);
	SysPrintf("len=%d\r\n",UART1_Rx_cnt);
	UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
	SysPrintf("Recv:%s\n",UART1_Rx_Buf);
	
	for(i=0;i<UART1_Rx_cnt;i++)
	{
		if(UART1_Rx_Buf[i] == ':')
		{
			if(UART1_Rx_Buf[i+2]=='0') return -1;
			else if(UART1_Rx_Buf[i+2]=='2') return 1;	
			else if(UART1_Rx_Buf[i+2]=='1') return 2;
			else return -1;
		}
	}
	return -1;
}


/*
*********************************************************************************************************
*	������:   M120_Join_Net_INT
*	����˵��: M120ģ������
*	����ֵ:   �����ɹ�����1������ʧ�ܷ���-1
*   ���ߣ�    ����
*   ���ڣ�    2021-10-19
*********************************************************************************************************/
int8_t M120_Join_Net_INT(void)
{
	int i=0;
	uint8_t j=12;
	SysPrintf("M120L join net>>>\r\n");
	UART1_Rx_cnt=0 ;
	SendData_To_M120L(&huart1,M120_enjoin_cmd_dma,18);  
	while(j--)
	{
		HAL_Delay(5000);
		SysPrintf("len=%d\r\n",UART1_Rx_cnt);
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		SysPrintf("Recv:%s\n",UART1_Rx_Buf);
		
		 if((strstr(UART1_Rx_Buf,"OK+JOINED")!=NULL))
		 {
			SysPrintf("M120L join sucess\r\n"); 
			return 1;
		 }
		 else if((strstr(UART1_Rx_Buf,"ERR+JOINED:0002")!=NULL))
		 {
			SysPrintf("M120L join fail\r\n"); 
			return -1; 
		 }
	}
	return -1;
}

/*
*********************************************************************************************************
*	������:    M120_TxData_INT
*	����˵��: ��Ҫ���͵�16�������ݣ�ת����ASCII�룬��lora����ȥ��
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2019-06-04
*********************************************************************************************************/
int8_t M120_TxData_INT(unsigned char* str, uint8_t length)
{
	uint8_t i;
	uint8_t j;
	uint8_t Valid_Data_Len;
	unsigned char Lora_TxData[128];
	j = 5;
	Lora_TxData[0]='A';
	Lora_TxData[1]='T';
	Lora_TxData[2]='+';
	Lora_TxData[3]='S';
	Lora_TxData[4]='E';
	Lora_TxData[5]='N';
	Lora_TxData[6]='D';
	Lora_TxData[7]='=';
	Lora_TxData[8]='1';
	Lora_TxData[9]=',';
	Lora_TxData[10]='2';
	Lora_TxData[11]=',';
	if(length<=Valid_Data_Len_Max)
		Valid_Data_Len = length;
	else
		Valid_Data_Len = Valid_Data_Len_Max;

	for(i=0;i<Valid_Data_Len;i++)
	{
		Lora_TxData[12+i] = str[i];
	}
	Lora_TxData[i+12] = '\r';
	Lora_TxData[i+13] = '\n';
	SysPrintf("M120L send data>>>\r\n");
	UART1_Rx_cnt=0 ;
	SendData_To_M120L(&huart1,Lora_TxData,i+14);
	while(j--)
	{
		HAL_Delay(5000);
		SysPrintf("len=%d\r\n",UART1_Rx_cnt);
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		SysPrintf("Recv:%s\n",UART1_Rx_Buf);
		
		 if((strstr(UART1_Rx_Buf,"OK+SENT")!=NULL))
		 {
			SysPrintf("M120L send data sucess\r\n"); 
			return 1;
		 }
		 else if((strstr(UART1_Rx_Buf,"ERR+SENT")!=NULL))
		 {
			SysPrintf("M120L send data fail\r\n"); 
			return -1; 
		 }
	}
	return -1;
}

/*
*********************************************************************************************************
*	������:    M120_RecvData_INT
*	����˵��: ����ָ��
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2019-06-04
*********************************************************************************************************/
uint8_t M120_RecvData_INT(void)
{
	uint8_t i,k;
	uint8_t begin_pos = 255;
	uint8_t end_pos = 0;
	uint8_t j = 3;
	Rece_len = 0;
	SysPrintf("M120L Recv data>>>\r\n");
	while(j--)
	{
		LED_blink(5); 
		HAL_Delay(1000);
		SysPrintf("len=%d\r\n",UART1_Rx_cnt);
		UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
		SysPrintf("Recv:%s\n",UART1_Rx_Buf);
		
		 if( (strstr(UART1_Rx_Buf,"7365746465763d")!=NULL) && (strstr(UART1_Rx_Buf,"4141")!=NULL) && (UART1_Rx_cnt>=34)) 
		 {
			for(i=0;i<UART1_Rx_cnt-20;i++)
			{
				if( (UART1_Rx_Buf[i]=='7') && (UART1_Rx_Buf[i+1]=='3') && (UART1_Rx_Buf[i+2]=='6') && (UART1_Rx_Buf[i+3]=='5') && (UART1_Rx_Buf[i+4]=='7') &&(UART1_Rx_Buf[i+5]=='4') &&(UART1_Rx_Buf[i+6]=='6')&&(UART1_Rx_Buf[i+7]=='4')&&(UART1_Rx_Buf[i+8]=='6')&&(UART1_Rx_Buf[i+9]=='5')&&(UART1_Rx_Buf[i+10]=='7')&&(UART1_Rx_Buf[i+11]=='6')&&(UART1_Rx_Buf[i+12]=='3')&&(UART1_Rx_Buf[i+13]=='d'))
				{
					begin_pos = i;
					SysPrintf("begin_pos=%d\r\n",begin_pos);
					break;
				}
			}
			
			for(i=0;i<UART1_Rx_cnt-3;i++)
			{
				if( (UART1_Rx_Buf[i]=='4') && (UART1_Rx_Buf[i+1]=='1') && (UART1_Rx_Buf[i+2]=='4') && (UART1_Rx_Buf[i+3]=='1'))
				{
					end_pos = i+3;
					SysPrintf("end_pos=%d\r\n",end_pos);
					break;
				}
			}
			
			if( (end_pos>begin_pos) && (begin_pos<255) && (end_pos>0))
			{
				if( (end_pos-begin_pos+1)%2==0)
				{
					Rece_len = (end_pos - begin_pos + 1)>>1;
					if(Rece_len<ReceiveUdpDataMaxLen)
					{
						for(i=0;i<Rece_len;i++)
							Recebuffer[i] = asciitohex(UART1_Rx_Buf[begin_pos+i+i],UART1_Rx_Buf[begin_pos+i+i+1]);
						SysPrintf("Recv valid-len=%d\r\n",Rece_len);
						SysPrintf("Recv valid-cmd:");
						for(i=0;i<Rece_len;i++)
							SysPrintf("%c",Recebuffer[i]);
						SysPrintf("\r\n");
						return 1;
					}
					else
					{
						Rece_len = 0;
						SysPrintf("err cmd len exceed max len\r\n");
						return 2;
					}
					
				}
				else
				{
					Rece_len = 0;
					SysPrintf("M120 Recv data err format\r\n");	
					return 3;
				}
			}
			else
			{
				Rece_len = 0;
				SysPrintf("M120 Recv data err format\r\n");	
				return 3;
			}
		 }
		 else
		 {
			 ;
		 }			
	}
	SysPrintf("M120 Recv no data\r\n");
	Rece_len = 0;
	return 0;
}
/*
*********************************************************************************************************
*	������:   M120_sleep01_INT
*	����˵��: ��M120����ָ��ȴ�M120����
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2021-10-19
*********************************************************************************************************/
int8_t M120_sleep01_INT(void)
{
	SysPrintf("M120L sleep>>>\r\n");
	UART1_Rx_cnt = 0;
	SendData_To_M120L(&huart1,M120_sleep01_cmd,13);
	HAL_Delay(200);
	SysPrintf("len=%d\r\n",UART1_Rx_cnt);
	UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
	SysPrintf("Recv:%s\n",UART1_Rx_Buf);
	if((strstr(UART1_Rx_Buf,"OK+SLEEP:01")!=NULL))
	{
		SysPrintf("M120L sleep sucess\r\n"); 
		return 1;
	}
	else
	{
		SysPrintf("M120L sleep fail\r\n"); 
		return -1;
	}
	
}

/*
*********************************************************************************************************
*	������:    M120_Wakeup_INT
*	����˵��: M120L����
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2021-10-19
*********************************************************************************************************/
void M120_Wakeup_INT(void)
{
	int i;
	SysPrintf("Wake up M120L>>>\r\n");
	SETA_LOW;
	HAL_Delay(10);
	SETA_HIGH;
	HAL_Delay(200);
	M120_sleep00_INT();
}


